<?php

	/**
	 * Defines a class to be used for Date Data Typing with PHP
	 *  
	 * @author Pablo Santiago Sánchez <phackwer@gmail.com>
	 * @copyright Copyright (c) 2008, Pablo Santiago Sánchez
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage datatypes
	 */

	/**
	 * Class used to handle dates.
	 * Only YYYY-MM-DD format allowed.
	 * 
	 * @package pop
	 * @subpackage datatypes
	 */
	class PDate extends PTypeBase
	{
		/**
		 * Constructor, in which you must inform the default date.
		 * If none is informed, it uses TODAY as default
		 * @param string $defaultval Default value. Must be a valid date
		 */
		public function __construct($defaultval = TODAY)
		{
			$this->__set("value", $defaultval);
			$this->defaultval 	= $defaultval;
		}

		/**
		 * Magic method used to set the value and check if it's valid
		 * @param string $name name of the value
		 * @param string $value value itself
		 * @return bool 
		 */
		public function __set($name, $value)
		{
			if ($value === "")
				$value = null;

			if(strstr($value, " "))
			{
				$value = explode(" ",$value);
				$value = $value[0];
			}

			$date = explode("-",$value);

			if ($name != "value")
			{
				throw new Exception("This attribute cannot be set");
			}
			else if ((!is_null($this->defaultval) && !is_null($value)) && (count($date)==1 || !$date[1] || !$date[2] || !$date[0] || !checkdate($date[1],$date[2],$date[0])))
			{
				throw new Exception("This attribute must be a valid date.\nName: <font color=#FF0000>".$name."</font>\nValue: <font color=#FF0000>".$value."</font>");
			}
			else
			{
				$this->$name = $value;
				return true;
			}
		}
	}

?>
